import { MoveResult } from '@renderer/types/game'
import _ from 'lodash'

// 初始化游戏数据
export const initGameData = (gridSize: number) => {
  const data = []
  for (let i = 0; i < gridSize; i++) {
    const tmp = []
    for (let j = 0; j < gridSize; j++) {
      tmp.push(0)
    }
    data.push(tmp)
  }
  return data
}

// 生成随机空方块索引
export const generateNewBlockIndexes = (indexes: number[], num: number) => {
  if (indexes.length <= num) {
    return indexes
  }
  return _.shuffle(indexes).slice(0, num)
}

// 随机生成值
export const generateRadomValue = () => {
  const randomValue = _.random(3)
  return randomValue < 3 ? 1 : 2
}

// 一维数组索引转化为二位数组索引
export const increaseIndexDimension = (index: number, size: number): { row: number, col: number } => {
  return {
    row: Math.floor(index / size),
    col: index % size
  }
}

// 测试是否能执行向下操作
export const testMoveDown = (gridData: number[][]): MoveResult => {
  const result = _.cloneDeep(gridData)
  const size = gridData.length
  let canMove = false
  for (let i = size - 2; i >= 0; i--) {
    for (let j = 0; j < size; j++) {
      // 找到移动方向上最后一个连续为空的块
      let lastEmptyIndex = -1
      const value = result[i][j]
      for (let k = i + 1; k < size; k++) {
        if (result[k][j] === 0) {
          lastEmptyIndex = k
        } else {
          break
        }
      }
      // 没有连续为空的块
      if (lastEmptyIndex === -1) {
        // 判断是否能与移动方向上的相邻块合并
        if (value === result[i + 1][j]) {
          canMove = true
          result[i][j] = 0
          result[i + 1][j]++
        }
      } else { // 有连续为空的块
        // 判断是否能与空块的下一块合并
        if (lastEmptyIndex < size - 1 && value === result[lastEmptyIndex + 1][j]) {
          result[lastEmptyIndex + 1][j]++
        } else {
          // 将当前块移动到空块处
          result[lastEmptyIndex][j] = value
        }
        canMove = true
        result[i][j] = 0
      }
    }
  }
  return {
    canMove,
    result
  }
}

// 测试是否能执行向上操作
export const testMoveUp = (gridData: number[][]): MoveResult => {
  const result = _.cloneDeep(gridData)
  const size = gridData.length
  let canMove = false
  for (let i = 1; i < size; i++) {
    for (let j = 0; j < size; j++) {
      // 找到移动方向上最后一个连续为空的块
      let lastEmptyIndex = -1
      const value = result[i][j]
      for (let k = i - 1; k >= 0; k--) {
        if (result[k][j] === 0) {
          lastEmptyIndex = k
        } else {
          break
        }
      }
      // 没有连续为空的块
      if (lastEmptyIndex === -1) {
        // 判断是否能与移动方向上的相邻块合并
        if (value === result[i - 1][j]) {
          canMove = true
          result[i][j] = 0
          result[i - 1][j]++
        }
      } else { // 有连续为空的块
        // 判断是否能与空块的下一块合并
        if (lastEmptyIndex > 0 && value === result[lastEmptyIndex - 1][j]) {
          result[lastEmptyIndex - 1][j]++
        } else {
          // 将当前块移动到空块处
          result[lastEmptyIndex][j] = value
        }
        canMove = true
        result[i][j] = 0
      }
    }
  }
  return {
    canMove,
    result
  }
}

// 测试是否能执行向左操作
export const testMoveLeft = (gridData: number[][]): MoveResult => {
  const result = _.cloneDeep(gridData)
  const size = gridData.length
  let canMove = false
  for (let i = 0; i < size; i++) {
    for (let j = 1; j < size; j++) {
      // 找到移动方向上最后一个连续为空的块
      let lastEmptyIndex = -1
      const value = result[i][j]
      for (let k = j - 1; k >= 0; k--) {
        if (result[i][k] === 0) {
          lastEmptyIndex = k
        } else {
          break
        }
      }
      // 没有连续为空的块
      if (lastEmptyIndex === -1) {
        // 判断是否能与移动方向上的相邻块合并
        if (value === result[i][j - 1]) {
          canMove = true
          result[i][j] = 0
          result[i][j - 1]++
        }
      } else { // 有连续为空的块
        // 判断是否能与空块的下一块合并
        if (lastEmptyIndex > 0 && value === result[i][lastEmptyIndex - 1]) {
          result[i][lastEmptyIndex - 1]++
        } else {
          // 将当前块移动到空块处
          result[i][lastEmptyIndex] = value
        }
        canMove = true
        result[i][j] = 0
      }
    }
  }
  return {
    canMove,
    result
  }
}

// 测试是否能执行向右操作
export const testMoveRight = (gridData: number[][]): MoveResult => {
  const result = _.cloneDeep(gridData)
  const size = gridData.length
  let canMove = false
  for (let i = 0; i < size; i++) {
    for (let j = size - 2; j >= 0; j--) {
      // 找到移动方向上最后一个连续为空的块
      let lastEmptyIndex = -1
      const value = result[i][j]
      for (let k = j + 1; k < size; k++) {
        if (result[i][k] === 0) {
          lastEmptyIndex = k
        } else {
          break
        }
      }
      // 没有连续为空的块
      if (lastEmptyIndex === -1) {
        // 判断是否能与移动方向上的相邻块合并
        if (value === result[i][j + 1]) {
          canMove = true
          result[i][j] = 0
          result[i][j + 1]++
        }
      } else { // 有连续为空的块
        // 判断是否能与空块的下一块合并
        if (lastEmptyIndex > 0 && value === result[i][lastEmptyIndex + 1]) {
          result[i][lastEmptyIndex + 1]++
        } else {
          // 将当前块移动到空块处
          result[i][lastEmptyIndex] = value
        }
        canMove = true
        result[i][j] = 0
      }
    }
  }
  return {
    canMove,
    result
  }
}
